package cn.jbolt.admin.dept;

import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.api.httpmethod.JBoltHttpGet;
import cn.jbolt.core.api.httpmethod.JBoltHttpPost;
import cn.jbolt.core.model.Dept;
import cn.jbolt.core.model.base.JBoltBaseModel;
import cn.jbolt.core.permission.CheckPermission;
import cn.jbolt.core.permission.UnCheck;
import cn.jbolt.core.permission.UnCheckIfSystemAdmin;
import cn.jbolt.core.service.JBoltDeptService;
import cn.jbolt.core.service.base.JBoltBaseService;
import cn.jbolt.extend.permission.PermissionKey;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.kit.Okv;
import com.jfinal.plugin.activerecord.tx.Tx;

@UnCheckIfSystemAdmin
@CheckPermission(PermissionKey.DEPT)
@Path("/api/admin/dept")
public class DeptAdminController extends JBoltApiBaseController {

    @Inject
    private JBoltDeptService service;

    @Override
    protected JBoltBaseService<? extends JBoltBaseModel<?>> getModelService() {
        return service;
    }

    /**
     * 单个详情
     */
    @JBoltHttpGet
    public void index() {
       renderJBoltApiRet(findModelById());
    }
    /**
     * treeTable数据源 管理用
     * 适用于数据量少的情况
     */
    @JBoltHttpGet
    public void allTreeTableDatas() {
        renderJBoltApiSuccessWithData(service.getTreeTableDatas(null));
    }

    /**
     * treeTable数据源 显示enable=true的
     * 适用于数据量少的情况
     */
    @UnCheck
    @JBoltHttpGet
    public void enableTreeTableDatas() {
        renderJBoltApiSuccessWithData(service.getTreeTableDatas(true));
    }

    /**
     * 搜索接口 返回分页列表数据
     * @param page
     * @param pageSize
     * @param enable
     * @param keywords
     */
    public void search(@Para(value = PAGE_PARAM_NAME,defaultValue = "1")Integer page,@Para(value = PAGESIZE_PARAM_NAME,defaultValue = "15")Integer pageSize,@Para("enable")Boolean enable,@Para("keywords")String keywords){
        renderJBoltApiSuccessWithData(service.paginateByKeywords("id","asc",page,pageSize,keywords,"name,full_name,sn,leader,phone,email,zipcode,remark", Okv.create().setIfNotNull("enable", enable)));
    }

    /**
     * 通用条件查询
     * @param async 是否异步treeTable
     * @param pid   异步查询时候使用 上级ID
     * @param enable  是否启用
     */
    @JBoltHttpGet
    public void datas(@Para(value = "async",defaultValue = "false")Boolean async,@Para(PID_PARAM_NAME)Long pid,@Para(ENABLE_PARAM_NAME)Boolean enable) {
        if(isOk(pid)){
            if(enable == null){
                renderJBoltApiSuccessWithData(service.getSonsOrderBySortRank(pid));
                return;
            }
            if(enable){
                renderJBoltApiSuccessWithData(service.getEnableSonsOrderBySortRank(pid));
            }else{
                renderJBoltApiSuccessWithData(service.getNotEnableSonsOrderBySortRank(pid));
            }
        }else{
            if(async){
                renderJBoltApiSuccessWithData(service.getAllParentsOrderBySortRank(enable));
            }else{
                renderJBoltApiSuccessWithData(service.getTreeTableDatas(enable));
            }
        }

    }

    /**
     * select数据源 只需要enable=true的
     */
    @UnCheck
    @JBoltHttpGet
    public void enableOptions() {
        renderJBoltApiSuccessWithData(service.getTreeDatas(true,true));
    }



    /**
     * 保存
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void save(@Para("")Dept dept) {
        renderJBoltApiRet(service.save(dept));
    }

    /**
     * 更新
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void update(@Para("")Dept dept) {
        renderJBoltApiRet(service.update(dept));
    }

    /**
     * 删除
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void delete() {
        renderJBoltApiRet(deleteModelById(true));
    }

    /**
     * 切换启用状态
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void toggleEnable() {
        renderJBoltApiRet(toggleModelEnableById());
    }

    /**
     * 排序 上移
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void up() {
        renderJBoltApiRet(service.up(getId()));
    }

    /**
     * 排序 下移
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void down() {
        renderJBoltApiRet(service.down(getId()));
    }

    /**
     * 排序 初始化
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void initRank() {
        renderJBoltApiRet(service.initRank());
    }

    /**
     * 处理所有部门的deptPath
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void processAllDeptPath() {
        service.processAllDeptPath();
        renderJBoltApiSuccess();
    }

    /**
     * jstree 节点move
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void move(@Para("id")Long id,@Para("pid")Long pid,@Para("rank")Integer rank){
        renderJBoltApiRet(service.move(id,pid,rank));
    }
}
